﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmKetQuaHocTap : FormBase
    {
        public frmKetQuaHocTap()
        {
            InitializeComponent();
        }

        public int NamThucTapID { get; set; }

        private void frmKetQuaHocTap_Load(object sender, EventArgs e)
        {
            try
            {
                NamThucTapID = -1;
                LoadNamThucTap();
                LoadSinhVien();
                LoadGiangVien();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void cboNamThucTap_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                NamThucTapID = Convert.ToInt32(cboNamThucTap.EditValue);
                LoadSinhVien();
                LoadGiangVien();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void LoadNamThucTap()
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(-1, "--- Chọn năm thực tập ---");
            var query = Session.DefaultSession.Query<NamThucTap>().Select(c => new
            {
                c.Oid,
                c.NamHoc
            });
            foreach (var item in query)
            {
                dic.Add(item.Oid, item.NamHoc);
            }
            repositoryItemLookUpEdit1.ValueMember = "Key";
            repositoryItemLookUpEdit1.DisplayMember = "Value";
            repositoryItemLookUpEdit1.DataSource = dic;
            cboNamThucTap.EditValue = NamThucTapID;
        }

        private void LoadSinhVien()
        {
            DevExpress.Data.Filtering.CriteriaOperator criteria = DevExpress.Data.Filtering.CriteriaOperator.Parse("GiangVienTrongNam.NamThucTap.Oid=" + NamThucTapID);
            xpCollection1.Criteria = criteria;
        }

        private void LoadGiangVien()
        {
            DevExpress.Data.Filtering.CriteriaOperator criteria = DevExpress.Data.Filtering.CriteriaOperator.Parse("NamThucTap.Oid=" + NamThucTapID);
            xpCollection3.Criteria = criteria;
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (NamThucTapID != -1 && gridView1.GetFocusedRow() != null)
                {
                    var obj = xpCollection1[gridView1.GetDataSourceRowIndex(gridView1.FocusedRowHandle)] as SinhVienTrongGiangVien;
                    frmKetQuaHocTap_SinhVien frm = new frmKetQuaHocTap_SinhVien();
                    frm.NamThucTapID = NamThucTapID;
                    frm.SinhVienID = obj.SinhVien.Oid;
                    if (frm.ShowDialog() == DialogResult.OK)
                    {

                    }
                }
                else
                    CommonMessage("Chưa chọn năm thực tập");
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void gridView2_DoubleClick(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void gridView3_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (NamThucTapID != -1 && gridView3.GetFocusedRow() != null)
                {
                    var obj = xpCollection3[gridView3.GetDataSourceRowIndex(gridView3.FocusedRowHandle)] as GiangVienTrongNam;
                    frmKetQuaHocTap_GiangVien frm = new frmKetQuaHocTap_GiangVien();
                    frm.NamThucTapID = NamThucTapID;
                    frm.GiangVienID = obj.Oid;
                    if (frm.ShowDialog() == DialogResult.OK)
                    {

                    }
                }
                else
                    CommonMessage("Chưa chọn năm thực tập");
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (NamThucTapID != -1)
                {
                    frmKetQuaHocTap_Import frm = new frmKetQuaHocTap_Import();
                    frm.NamThucTapID = NamThucTapID;
                    if (frm.ShowDialog() == DialogResult.OK)
                    {

                    }
                }
                else
                    CommonMessage("Chưa chọn năm thực tập");
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }
    }
}